package com.xxx.crypto;

import com.xxx.para.ParametersUtil;
import lombok.extern.slf4j.Slf4j;

import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Base64;

@Slf4j
public class Decript {

	/**
	 * 密钥
	 */
	private final static String SECRET_KEY="SECRET_KEY";


	/**
	 *
	 * @Method: encrypt
	 * @Description: 加密数据
	 * @param data
	 * @return
	 * @throws Exception
	 * @date 2016年7月26日
	 */
	public static String encrypt(String data) {  //对string进行BASE64Encoder转换
		if(!ParametersUtil.isNull(data)) {
			try {
				byte[] bt = encryptAES(data.getBytes(), SECRET_KEY);
				String strs =Base64.getEncoder().encodeToString(bt);
				return strs;
			}catch (Exception e){
				if(log.isErrorEnabled()){
					log.error(e.getMessage(),e);
				}
			}

		}
		return null;
	}
	/**
	 *
	 * @Method: encrypt
	 * @Description: 解密数据
	 * @param data
	 * @return
	 * @throws Exception
	 * @date 2016年7月26日
	 */
	public static String decryptor(String data){  //对string进行BASE64Encoder转换
		if(!ParametersUtil.isNull(data)) {
			try {
				byte[] bt = decryptAES(Base64.getDecoder().decode(data), SECRET_KEY);
				String strs = new String(bt);
				return strs;
			}catch (Exception e){
				if(log.isErrorEnabled()){
					log.error(e.getMessage(),e);
				}
			}
		}
		return null;
	}
	/**
	 * 加密
	 *
	 * @param content
	 *            需要加密的内容
	 * @param password
	 *            加密密码
	 * @return
	 */
	private static byte[] encryptAES(byte[] content, String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			kgen.init(128, random);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			// 创建密码器
			Cipher cipher = Cipher.getInstance("AES");
			byte[] byteContent = content;
			// 初始化
			cipher.init(Cipher.ENCRYPT_MODE, key);
			byte[] result = cipher.doFinal(byteContent);
			// 加密
			return result;
	}

	/**
	 * 解密
	 *
	 * @param content
	 *            待解密内容
	 * @param password
	 *            解密密钥
	 * @return
	 */
	private static byte[] decryptAES(byte[] content, String password) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
			KeyGenerator kgen = KeyGenerator.getInstance("AES");
			SecureRandom random = SecureRandom.getInstance("SHA1PRNG");
			random.setSeed(password.getBytes());
			kgen.init(128, random);
			SecretKey secretKey = kgen.generateKey();
			byte[] enCodeFormat = secretKey.getEncoded();
			SecretKeySpec key = new SecretKeySpec(enCodeFormat, "AES");
			// 创建密码器
			Cipher cipher = Cipher.getInstance("AES");
			// 初始化
			cipher.init(Cipher.DECRYPT_MODE, key);
			byte[] result = cipher.doFinal(content);
			// 加密
			return result;
	}





	public static void main(String[] args) {

		String[] s = {"0001","0002","0003","0002787","0002999","0002888","0002777","0002666","0002555","0002444","0002333","0002222","0002111","000299","000288","000277","000266","000255","000244","000233","000222","000211","00029","00028","00027","00026","00025","00024","00023","00022","00021"};
		for (String s1 : s) {
			try {
				String aaa = encrypt(s1);
				System.out.println(s1 +"   ====   "+aaa);
				//System.out.println(decryptor(aaa));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}


	}






}